package a66weding.com.jiehuntong.activity;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import a66weding.com.jiehuntong.dao.db.address.AddressDao;
import a66weding.com.jiehuntong.model.CityModel;
import a66weding.com.jiehuntong.model.DistrictModel;
import a66weding.com.jiehuntong.model.ProvinceModel;
import a66weding.com.jiehuntong.services.XmlParserHandler;
import a66weding.com.jiehuntong.widgets.wheelview.adapters.ArrayWheelAdapter;

/**
 * Created by Administrator on 2016/7/15.
 */
public class CityActivity extends Activity {
    private AddressDao addressDao;

    /**
     * 所有省
     */
    protected String[] mProvinceDatas;

    protected ArrayList<String> optionsProvince = new ArrayList<>();
    /**
     * key - 省 value - 市
     */
    protected Map<String, Map<String, String[]>> mCitisDatasMap = new HashMap<String, Map<String, String[]>>();

    /**
     * key - 市 values - 区
     */
    protected Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();

    /**
     * key - 区 values - 邮编
     */
    protected Map<String, String> mZipcodeDatasMap = new HashMap<String, String>();

    /**
     * 当前省的名称
     */
    protected String mCurrentProviceName;
    /**
     * 当前市的名称
     */
    protected String mCurrentCityName;
    /**
     * 当前区的名称
     */
    protected String mCurrentDistrictName = "";

    /**
     * 当前区的邮政编码
     */
    protected String mCurrentZipCode = "";


    /**
     * 解析省市区的XML数据
     */
    protected void initProvinceDatas() {


        List<ProvinceModel> provinceList = null;
        AssetManager asset = getResources().getAssets();

        try {
            InputStream input = asset.open("province_data.xml");
            // 创建一个解析xml的工厂对象
            SAXParserFactory spf = SAXParserFactory.newInstance();
            // 解析xml
            SAXParser parser = spf.newSAXParser();
            XmlParserHandler handler = new XmlParserHandler();
            parser.parse(input, handler);
            input.close();
            // 获取解析出来的数据
            provinceList = handler.getDataList();
            //*/ 初始化默认选中的省、市、区
            if (provinceList != null && !provinceList.isEmpty()) {
                mCurrentProviceName = provinceList.get(0).getName();
                List<CityModel> cityList = provinceList.get(0).getCityList();
                if (cityList != null && !cityList.isEmpty()) {
                    mCurrentCityName = cityList.get(0).getName();
                    List<DistrictModel> districtList = cityList.get(0).getDistrictList();
                    mCurrentDistrictName = districtList.get(0).getName();
                    mCurrentZipCode = districtList.get(0).getZipcode();
                }
            }


            //*/
            mProvinceDatas = new String[provinceList.size()];
            for (int i = 0; i < provinceList.size(); i++) {
                // 遍历所有省的数据
                mProvinceDatas[i] = provinceList.get(i).getName();
                List<CityModel> cityList = provinceList.get(i).getCityList();
                String[] cityNames = new String[cityList.size()];
                mDistrictDatasMap=new HashMap<String, String[]>();
                for (int j = 0; j < cityList.size(); j++) {
                    // 遍历省下面的所有市的数据
                    cityNames[j] = cityList.get(j).getName();
                    List<DistrictModel> districtList = cityList.get(j).getDistrictList();
                    String[] distrinctNameArray = new String[districtList.size()];
                    DistrictModel[] distrinctArray = new DistrictModel[districtList.size()];
                    ArrayList<String> cityName = new ArrayList<>();

                    String [] discArr=new String[districtList.size()];
                    for (int k = 0; k < districtList.size(); k++) {
                        // 遍历市下面所有区/县的数据
                        DistrictModel districtModel = new DistrictModel(districtList.get(k).getName(), districtList.get(k).getZipcode());
                        // 区/县对于的邮编，保存到mZipcodeDatasMap
                        mZipcodeDatasMap.put(districtList.get(k).getName(), districtList.get(k).getZipcode());
                        distrinctArray[k] = districtModel;
                        distrinctNameArray[k] = districtModel.getName();

                        discArr[k]=districtList.get(k).getName();

                    }

                    cityName.add(cityNames[j]);

                    mDistrictDatasMap.put(cityNames[j],discArr );
                }
                // 省-市的数据，保存到mCitisDatasMap
                mCitisDatasMap.put(provinceList.get(i).getName(), mDistrictDatasMap);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {

        }

    }


    /**
     * 解析省市区的XML数据
     */
    protected void initProvinceDatas2() {
        addressDao = new AddressDao(this);

        Set<String> provinceListSet = addressDao.getProvinceList();
        ArrayList<String> provinceList = new ArrayList<>(provinceListSet);
        optionsProvince = provinceList;
        mProvinceDatas = new String[provinceList.size()];
        for (int i = 0; i < provinceList.size(); i++) {
            mProvinceDatas[i] = provinceList.get(i);
            Set<String> cityListByPName = addressDao.getCityListByPName(provinceList.get(i));
            List<String> cityList = new ArrayList<>(cityListByPName);
            String[] cityNames = new String[cityList.size()];
            mCurrentProviceName = provinceList.get(0);
            mDistrictDatasMap=new HashMap<String, String[]>();
            for (int j = 0; j < cityList.size(); j++) {

                cityNames[j] = cityList.get(j);

                Set<String> areaListByPName = addressDao.getAreaListByPNameCName(provinceList.get(i), cityList.get(j));
                if ("县".equals(cityList.get(j))) {
                }
                List<String> districtList = new ArrayList<String>(areaListByPName);
                String[] distrinctNameArray = new String[districtList.size()];
                mCurrentCityName = cityList.get(0);
                ArrayList<String> cityName = new ArrayList<>();

                String [] discArr=new String[districtList.size()];
                for (int k = 0; k < districtList.size(); k++) {
                    // 遍历市下面所有区/县的数据
                    DistrictModel districtModel = new DistrictModel(districtList.get(k), "");
                    distrinctNameArray[k] = districtList.get(k);

                    mCurrentDistrictName = districtList.get(0);

                    cityName.add(cityNames[j]);

                    discArr[k]=districtList.get(k);

                }
                mDistrictDatasMap.put(cityNames[j],discArr );


            }

            mCitisDatasMap.put(provinceList.get(i), mDistrictDatasMap);

        }
    }
}
